package com.warmlight.voicepacket.fragment;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.design.widget.BottomSheetDialogFragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMWeb;
import com.warmlight.voicepacket.R;
import com.warmlight.voicepacket.data.PlaydeatalData;

@SuppressLint("ValidFragment")
public class CustomBottomSheetDialogFragment extends BottomSheetDialogFragment implements View.OnClickListener{
    public interface ShareCallBack{
        void shareSuccess(boolean isSuccess);
    }

    private String name;


    private PlaydeatalData.DataBean.ShareBean shareBean;
    private ShareCallBack shareCallBack;
    private boolean isLock;
    @SuppressLint("ValidFragment")
    public CustomBottomSheetDialogFragment(PlaydeatalData.DataBean.ShareBean shareBean) {
        this.shareBean = shareBean;
    }

    public CustomBottomSheetDialogFragment(PlaydeatalData.DataBean.ShareBean shareBean,String name) {
        this.shareBean = shareBean;
        this.name = name;
    }


    public CustomBottomSheetDialogFragment(PlaydeatalData.DataBean.ShareBean shareBean,ShareCallBack shareCallBack,boolean isLock){
        this.shareBean = shareBean;
        this.shareCallBack = shareCallBack;
        this.isLock = isLock;
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.layout_shareplay, container, false);
        initView(v);
        return v;
    }
    private void initView(View v){
//        Log.d( "share: ",shareBean.getDesc());
                View ll_sharewx = v.findViewById(R.id.ll_sharewx);
        View ll_shareqq = v.findViewById(R.id.ll_shareqq);
        View ll_sharewb = v.findViewById(R.id.ll_sharewb);
        TextView tv_lock_hint = (TextView) v.findViewById(R.id.tv_lock_hint);
        TextView bottomsheet_text = (TextView) v.findViewById(R.id.bottomsheet_text);
        if (isLock){
            tv_lock_hint.setVisibility(View.VISIBLE);
            bottomsheet_text.setText("分享后才能解锁哦");
        }else{
            tv_lock_hint.setVisibility(View.GONE);
            bottomsheet_text.setText("分享好友");
        }
        ll_sharewx.setOnClickListener(this);
        ll_shareqq.setOnClickListener(this);
        ll_sharewb.setOnClickListener(this);
    }
    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.ll_sharewx:
//                Log.e("onClick: '",Wechat.NAME );
                share(SHARE_MEDIA.WEIXIN);
                dismiss();
                break;
            case R.id.ll_shareqq:
                share(SHARE_MEDIA.QQ);
                dismiss();
                break;
            case R.id.ll_sharewb:
                share(SHARE_MEDIA.WEIXIN_CIRCLE);
                dismiss();
                break;
        }

    }


    private void share(SHARE_MEDIA weixinCircle){
        String nick = "";
        if (!TextUtils.isEmpty(name)){
            nick = "&passuser="+name;
        }
        UMWeb web = new UMWeb(shareBean.getH5_url()+nick);
        web.setTitle(shareBean.getTitle());//标题
        if (shareBean.getPic_url().contains("http")) {
//            UMImage image = new UMImage(ShareActivity.this, "imageurl");//网络图片
//            UMImage image = new UMImage(ShareActivity.this, file);//本地文件
//            UMImage image = new UMImage(ShareActivity.this, R.drawable.xxx);//资源文件
//            UMImage image = new UMImage(ShareActivity.this, bitmap);//bitmap文件
//            UMImage image = new UMImage(ShareActivity.this, byte[]);//字节流
            UMImage thumb =  new UMImage(getContext(), shareBean.getPic_url());
//            image.setThumb(thumb);
            web.setThumb(thumb);  //缩略图
        }
        web.setDescription(shareBean.getDesc());//描述
        new ShareAction(getActivity())
                .setPlatform(weixinCircle)//传入平台
                .withMedia(web)//分享内容
                .setCallback(new UMShareListener() {
                    @Override
                    public void onStart(SHARE_MEDIA share_media) {

                    }

                    @Override
                    public void onResult(SHARE_MEDIA share_media) {
                        if (shareCallBack != null){
                            shareCallBack.shareSuccess(true);
                        }

                    }

                    @Override
                    public void onError(SHARE_MEDIA share_media, Throwable throwable) {
                        if (shareCallBack != null){
                            shareCallBack.shareSuccess(false);
                        }
                    }

                    @Override
                    public void onCancel(SHARE_MEDIA share_media) {
                        if (shareCallBack != null){
                            shareCallBack.shareSuccess(false);
                        }
                    }
                })//回调监听器
                .share();
    }


//    private void share(String pn){
//
//        Platform.ShareParams sp = new Platform.ShareParams();
//        sp.setTitle(shareBean.getTitle());
//        sp.setTitleUrl(shareBean.getH5_url()); // 标题的超链接
//        sp.setText(shareBean.getDesc());
//
//        sp.setSite("暖光科技");
//        sp.setSiteUrl(shareBean.getH5_url());
//
//        sp.setUrl(shareBean.getH5_url());
//
//
//        //微信的
//        if (shareBean.getPic_url().contains("http")) {
//            sp.setImageUrl(shareBean.getPic_url());
//        }
//
//        sp.setShareType(Platform.SHARE_WEBPAGE);
//        Platform qzone = ShareSDK.getPlatform (pn);
//// 设置分享事件回调（注：回调放在不能保证在主线程调用，不可以在里面直接处理UI操作）
//        qzone.setPlatformActionListener (new PlatformActionListener() {
//            public void onError(Platform arg0, int arg1, Throwable arg2) {
////                Log.e("onError: ", arg2.toString());
//                //失败的回调，arg:平台对象，arg1:表示当前的动作，arg2:异常信息
//                if (shareCallBack != null){
//                    shareCallBack.shareSuccess(false);
//                }
//            }
//            public void onComplete(Platform arg0, int arg1, HashMap arg2) {
//                //分享成功的回调
//                if (shareCallBack != null){
//                    shareCallBack.shareSuccess(true);
//                }
//            }
//            public void onCancel(Platform arg0, int arg1) {
//                //取消分享的回调
//                if (shareCallBack != null){
//                    shareCallBack.shareSuccess(false);
//                }
//            }
//        });
//// 执行图文分享
//        qzone.share(sp);
//    }
}